<?php

/**
 * Front controller plugin
 * Set Language plugin
 *
 * @uses       Zend_Controller_Plugin_Abstract
 *
 * @category   Core
 * @package    Core_Controller
 * @subpackage Plugin
 *
 * @version $Id$
 */
class Core_Controller_Plugin_Translate extends Zend_Controller_Plugin_Abstract
{

    /**
     * Options
     *
     * @var array
     */
    private $_options;

    /**
     * Contructor
     * Verify options
     *
     * @param array $options
     */
    public function __construct(Array $options = array())
    {
        if (!sizeof($options)) {
           throw new Core_Exception('No translate plugin config found in bootstrap.ini');
        }
        $this->_options = $options;
    }

    /**
     * init translate cache
     *
     */
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        if (Zend_Registry::get('Zend_Cache_Manager')->hasCache('translate')) {
            Zend_Translate::setCache(
                Zend_Registry::get('Zend_Cache_Manager')->getCache('translate')
            );
        }

        if (($translator = Zend_Registry::isRegistered('Zend_Translate')) === false) {
            $translator = $this->defineTranslator();
            Zend_Registry::set('Zend_Translate', $translator);
        }
        
        $this->addTranslator($translator);

        Zend_Validate_Abstract::setDefaultTranslator($translator);
        Zend_Form::setDefaultTranslator($translator);
    }

    /**
     * Define translator
     * 
     * @param Zend_Controller_Request_Abstract $request
     * @return Zent_Translate
     */
    protected function defineTranslator()
    {
        $router    = Zend_Controller_Front::getInstance()->getRouter();
        $module    = $router->getRoute('default')->getDefault('module');

        $translate = $this->getTranslatorConfig($module);

        $translator = new Zend_Translate(
            $this->_options['adapter'],
            $translate->path,
            $translate->language
        );

        return $translator;
    }

    /**
     * Add language file to translator
     *
     * @param Zend_Translate $translator
     */
    protected function addTranslator(Zend_Translate $translator)
    {
        $module    = $this->getRequest()->getParam('module');
        $translate = $this->getTranslatorConfig($module);

        $translator->addTranslation($translate->path, $translate->language);
    }

    /**
     * Get translator config
     * @param string $module
     * @return Zend_Config
     */
    protected function getTranslatorConfig($module)
    {
        $translate    = $this->getRequest()->getParam('translate');
        $languagePath = Zend_Controller_Front::getInstance()->getModuleDirectory() .
                DIRECTORY_SEPARATOR . 'languages/lang.';

        if (!file_exists($languagePath . $translate)) {
            $router    = Zend_Controller_Front::getInstance()->getRouter();
            $translate = $router->getRoute('default')->getDefault('translate');
        }
        
        $config = new Zend_Config(array(
            'language'=>$translate,
            'path'=>$languagePath . $translate
        ));
        
        return $config;
    }
}
